package com.wits.qso.dao.impl;

import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Component;

import com.wits.platform.dao.impl.BaseDaoImpl;
import com.wits.qso.dao.custom.MaterialdDaoCustom;
import com.wits.qso.entity.Course;
import com.wits.qso.entity.MaterialD;

@Component
public class MaterialdDaoImpl extends BaseDaoImpl<MaterialD, String> implements MaterialdDaoCustom
{
	public MaterialD findByMaterialHId(String materialHId) {
		Query query = em.createQuery( "FROM MaterialD o WHERE o.mtrhId = :id" );
		query.setParameter( "id", materialHId );

		return (MaterialD) query.getSingleResult();
	}
	
	public List<MaterialD> findByCourse(Course course) {
		Query query = em.createQuery( "FROM MaterialD o WHERE o.mtrhId in ( SELECT h.id FROM MaterialH h WHERE h.couid.title = :courseName )" );
		query.setParameter( "courseName", course.getTitle() );
		
		return (List<MaterialD>) query.getResultList();
	}
	
	public int deleteById(String id) {
		Query query = em.createQuery( "DELETE FROM MaterialD o WHERE o.id = :id" );
		query.setParameter( "id", id );
		
		return query.executeUpdate();
	}
}
